package cn.scu.ne04jdemo.controller;

import cn.scu.ne04jdemo.entitiy.RetroactiveEarningRight;
import cn.scu.ne04jdemo.service.RetroactiveEarningRightService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
public class RetroactiveEarningRightController {
    private final RetroactiveEarningRightService retroactiveEarningRightService;

    public RetroactiveEarningRightController(RetroactiveEarningRightService retroactiveEarningRightService) {
        this.retroactiveEarningRightService = retroactiveEarningRightService;
    }

    @GetMapping("/retroactiveEarningRight")
    public ResponseEntity<IPage> getRetroactiveEarningRightList(@RequestParam(required = false) String userName,
                                                                @RequestParam(required = false) String startTime,
                                                                @RequestParam(required = false) String endTime,
                                                                @RequestParam(defaultValue = "1") int pageNum,
                                                                @RequestParam(defaultValue = "10") int pageSize) {
        LambdaQueryWrapper<RetroactiveEarningRight> queryWrapper = new QueryWrapper<RetroactiveEarningRight>().lambda()
                .like(StringUtils.isNotBlank(userName), RetroactiveEarningRight::getUserName, userName)
                .ge(StringUtils.isNotBlank(startTime), RetroactiveEarningRight::getEarningTime, startTime)
                .le(StringUtils.isNotBlank(endTime), RetroactiveEarningRight::getEarningTime, endTime);
        IPage<RetroactiveEarningRight> page = retroactiveEarningRightService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return ResponseEntity.ok(page);
    }

    @GetMapping("/retroactiveEarningRight/download")
    @ResponseBody
    public void download(@RequestParam String userName, HttpServletResponse response) {
        QueryWrapper<RetroactiveEarningRight> params = new QueryWrapper();
        params.like(org.apache.commons.lang.StringUtils.isNotBlank(userName), "user_name", userName);
        List<RetroactiveEarningRight> list = retroactiveEarningRightService.list(params);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("收益权追溯.xlsx", "UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            EasyExcel.write(response.getOutputStream(), RetroactiveEarningRight.class).sheet().doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
